﻿using UnityEngine;

namespace Runtime.Services.Battle.Behaviour {

    /// <summary>
    /// 攻击行为
    /// </summary>
    public interface IDFR_BattleBehaviour_Attack : IDFR_BattleBehaviour {

        /// <summary>
        /// 实体
        /// </summary>
        new public interface IEntity : IDFR_BattleBehaviour.IEntity {

            /// <summary>
            /// CD总量
            /// </summary>
            IDFR_BattleData CoolDownBase { get; }

            /// <summary>
            /// CD经过的时间
            /// </summary>
            IDFR_BattleData CoolDownDuration { get; }
        }

        public class Parameter {

            /// <summary>
            /// 关注对象位置提供者
            /// </summary>
            public IDFR_BattleWorldPositionProvider LookAtWorldPositionProvider;
        }

        public class Info : DFR_BattleBehaviourInfo<Parameter> {
        }
    }

    [Implement(typeof(IDFR_BattleBehaviour_Attack))]
    internal sealed class DFR_BattleBehaviour_Attack : DFR_BattleBehaviour<IDFR_BattleBehaviour_Attack.Info, IDFR_BattleBehaviour_Attack.Parameter>, IDFR_BattleBehaviour_Attack {

        new private IDFR_BattleBehaviour_Attack.IEntity Entity => (IDFR_BattleBehaviour_Attack.IEntity)base.Entity;

        private IDFR_BattleWorldPositionProvider m_LookAtWorldPositionProvider;

        protected override void Initialize() {
        }

        protected override void Release() {
            m_LookAtWorldPositionProvider = null;
            base.Release();
        }

        protected override IDFR_BattleBehaviour_Attack.Info LogicFrame(IDFR_BattleBehaviour_Attack.Parameter parameter) {
            Entity.CoolDownDuration.Value -= Entity.CoolDownBase.Value;

            return new IDFR_BattleBehaviour_Attack.Info {
                Parameter = parameter,
                Type = DFR_BattleBehaviourType.Attack,
            };
        }

        protected override void Execute(IDFR_BattleBehaviour_Attack.Info info) {
            m_LookAtWorldPositionProvider = info.Parameter.LookAtWorldPositionProvider;
        }

        protected override void Update(IDFR_BattleBehaviour_Attack.Info info, float interpolation) {
            if (m_LookAtWorldPositionProvider == null) {
                return;
            }
            StateMachine.State = DFR_BattleEntityState.Attack;
            var targetWorldPosition = m_LookAtWorldPositionProvider.WorldPosition;
            var scale = Transform.localScale.y;
            if (Transform.position.x < targetWorldPosition.x) {
                Transform.localScale = new Vector3(-scale, scale, 1f);
            } else {
                Transform.localScale = new Vector3(scale, scale, 1f);
            }
            m_LookAtWorldPositionProvider = null;
        }
    }
}
